package com.iflytek.sbd.es.impl;




import com.iflytek.sbd.es.OriginalElasticsearchClient;
import com.iflytek.sbd.es.SpringBootElasticSearchClient;
import com.iflytek.sbd.po.User;
import com.iflytek.sbd.vo.ConditionVO;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: myes
 * @description:
 * @author: 陈英俊
 * @create: 2021-09-08 17:02
 */
@Service
public class SpringBootElasticSearchClientImpl implements SpringBootElasticSearchClient {


    @Resource
    private ElasticsearchOperations elasticsearchOperations;
    @Resource
    private OriginalElasticsearchClient elasticsearchClient;


    @Override
    public List<User> findAll(ConditionVO vo) {

        // 构建一个查询的请求
        NativeSearchQuery query = this.buildFinaAllQueryRequest(vo);
        SearchHits<User> search = elasticsearchOperations.search(query, User.class);
        return search.stream().map(SearchHit::getContent).collect(Collectors.toList());
    }


    public void createNativeSearch() {


        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.boolQuery()).withPageable(PageRequest.of(1, 1));
        NativeSearchQuery query = builder.build();
        elasticsearchOperations.search(query, User.class);
    }

    private NativeSearchQuery buildFinaAllQueryRequest(ConditionVO vo) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (StringUtils.isNotEmpty(vo.getName())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("name", vo.getName()));
        }
        if (vo.getLessAge() != 0) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gte(vo.getLessAge()));
        }
        if (vo.getGreaterAge() != 0) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(vo.getLessAge()));
        }
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder).withPageable(PageRequest.of(vo.getPageNum() - 1, vo.getPageSize()));
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        return new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(vo.getPageNum() - 1, vo.getPageSize()))
                .build();

    }


}
